var bzjdr = {
    setBtnStatus: function () {
        var row = $('#grid_bzjdr').datagrid('getSelected');
        if (row && row.id) {
            //未导入
            if (row.czzt == 'wdr') {
                $("#btn_bzjdr_import").show();//导入
                $("#btn_bzjdr_cx").hide();//撤销
                $("#btn_bzjdr_cz").hide();//重置
            }
            if (row.czzt == 'drcg') {
                $("#btn_bzjdr_import").hide();//导入
                $("#btn_bzjdr_cx").show();//撤销
                $("#btn_bzjdr_cz").hide();//重置
            }
            if (row.czzt == 'drsb') {
                $("#btn_bzjdr_import").hide();//导入
                $("#btn_bzjdr_cx").hide();//撤销
                $("#btn_bzjdr_cz").show();//重置
            }
            if (row.czzt == 'sccg') {
                $("#btn_bzjdr_import").show();//导入
                $("#btn_bzjdr_cx").hide();//撤销
                $("#btn_bzjdr_cz").hide();//重置
            }
            if (row.czzt == 'scsb') {
                $("#btn_bzjdr_import").hide();//导入
                $("#btn_bzjdr_cx").hide();//撤销
                $("#btn_bzjdr_cz").show();//重置
            }
        } else {
            $("#btn_bzjdr_import").hide();//导入
            $("#btn_bzjdr_cx").hide();//撤销
            $("#btn_bzjdr_cz").hide();//重置
        }
    }
}


$(function () {
    //加载企业机构
    $("#bzjdr_gldw").qyjgzcombobox();
    $('#grid_bzjdr').datagrid({
        url: $.webPath() + "/cwgl/bzjdr/bzjdrList?sfklx=3",
        toolbar: "#toolbar_bzjdr",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        pageNumber: 1,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'bm', title: '单据号', width: 25, align: 'center',formatter:skdrCell},
                {field: 'pjhm', title: '票据号码', width: 25, align: 'center'},
                {field: 'sfkrq', title: '收款日期', width: 25, align: 'center'},
                /*{field: 'sfklx', title: '收款类型', width: 15, align: 'center',
                    formatter: function (value) {
                        if (value == '3')
                            return '销售收款';
                        else
                            return '供应退款';
                    }
                },*/
                {field: 'gldw_qc', title: '关联企业', width: 30, align: 'center'},
                {field: 'sfje', title: '收款金额（元）', width: 20, align: 'right',
                    formatter: function (value) {
                        var sl = formatJe2(value, ",");
                        return sl;
                    },
                    styler: function () {
                        return 'color:green;font-size:14px;';
                    }
                },
                {field: 'kxsy_z', title: '款项用途', width: 25, align: 'center'},
                {field: 'pjlx_z', title: '票据类型', width: 15, align: 'center'},
                {field: 'nbkhh_qc', title: '入账银行', width: 15, align: 'center'},
                {field: 'shr_xm', title: '审核人', width: 12, align: 'center'},
                {field: 'sbgs_jc', title: '公司名称', width: 20, align: 'center'},
                {field: 'voucherNum', title: '凭证号', width: 15, align: 'center'},
                {field: 'czzt', title: '状态', align: 'center', width: 10, formatter: function (value, row, index) {
                    if (value == 'wdr')
                        return "未导入";
                    if (value == 'drcg')
                        return "导入成功";
                    if (value == 'drsb')
                        return "导入失败";
                    if (value == 'sccg')
                        return "删除成功";
                    if (value == 'scsb')
                        return "删除失败";
                    return "";
                }}
            ]
        ],
        onLoadSuccess: function () {
            var rows = $('#grid_bzjdr').datagrid("getRows");
            if (rows.length > 0) {
                $('#grid_bzjdr').datagrid("selectRow", 0);
            }
            bzjdr.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow: function () {
            bzjdr.setBtnStatus();
        }
    });
    /* //表格初始加载
     if ($("[name='bzjdr_type']:checked").val() == 1 || !$("[name='bzjdr_type']:checked").val()) {
     $("#kmgystype").text("供应商：");
     $('#grid_bzjdr').datagrid({
     url: $.webPath() + "/cwgl/bzjdr/bzjdrList",
     toolbar: "#toolbar_bzjdr",
     fitColumns: true,
     singleSelect: true,
     pagination: true,
     pageNumber: 1,
     fit: true,
     rownumbers: true,
     striped: true,
     columns: [
     [
     {field: 'bm', title: '单据号', width: 25, align: 'center'},
     {field: 'sfkrq', title: '付款日期', width: 25, align: 'center'},
     {field: 'sfklx', title: '付款类型', width: 15, align: 'center',
     formatter: function (value) {
     if (value == '1')
     return '供应付款';
     else
     return '销售退款';
     }
     },
     {field: 'gldw_qc', title: '关联企业', width: 30, align: 'center'},
     {field: 'sfje', title: '付款金额（元）', width: 20, align: 'right',
     formatter: function (value) {
     var sl = formatJe2(value, ",");
     return sl;
     },
     styler: function () {
     return 'color:green;font-size:14px;';
     }
     },
     {field: 'pjlx_z', title: '票据类型', width: 15, align: 'center'},
     {field: 'nbkhh_qc', title: '入账银行', width: 15, align: 'center'},
     {field: 'kxlx', title: '款项类型', width: 15, align: 'center',
     formatter: function (value) {
     if (value == '1')
     return '预付款';
     if (value == '2')
     return '应付款';
     if (value == '3')
     return '预收款';
     if (value == '4')
     return '应收款';
     }
     },
     {field: 'shr_xm', title: '审核人', width: 12, align: 'center'},
     {field: 'sbgs_jc', title: '公司名称', width: 20, align: 'center'},
     {field: 'voucherNum', title: '凭证号', width: 15, align: 'center'},
     {field: 'czzt', title: '状态', width: 10, align: 'center', formatter: function (value, row, index) {
     if (value == 'wdr')
     return "未导入";
     if (value == 'drcg')
     return "导入成功";
     if (value == 'drsb')
     return "导入失败";
     if (value == 'sccg')
     return "删除成功";
     if (value == 'scsb')
     return "删除失败";
     return "";
     }}
     ]
     ],
     onLoadSuccess: function () {
     var rows = $('#grid_bzjdr').datagrid("getRows");
     if (rows.length > 0) {
     $('#grid_bzjdr').datagrid("selectRow", 0);
     }
     bzjdr.setBtnStatus();
     $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
     },
     onClickRow: function () {
     bzjdr.setBtnStatus();
     }
     });
     } else {
     $("#kmgystype").text("客户：");
     $('#grid_bzjdr').datagrid({
     url: $.webPath() + "/cwgl/bzjdr/bzjdrList",
     toolbar: "#toolbar_bzjdr",
     fitColumns: true,
     singleSelect: true,
     pagination: true,
     pageNumber: 1,
     fit: true,
     rownumbers: true,
     striped: true,
     columns: [
     [
     {field: 'bm', title: '单据号', width: 25, align: 'center'},
     {field: 'sfkrq', title: '收款日期', width: 25, align: 'center'},
     {field: 'sfklx', title: '收款类型', width: 15, align: 'center',
     formatter: function (value) {
     if (value == '3')
     return '销售收款';
     else
     return '供应退款';
     }
     },
     {field: 'gldw_qc', title: '关联单位', width: 30, align: 'center'},
     {field: 'sfje', title: '付款金额（元）', width: 20, align: 'right',
     formatter: function (value) {
     var sl = formatJe2(value, ",");
     return sl;
     },
     styler: function () {
     return 'color:green;font-size:14px;';
     }
     },
     {field: 'kxlx', title: '款项类型', width: 15, align: 'center',
     formatter: function (value) {
     if (value == '1')
     return '预付款';
     if (value == '2')
     return '应付款';
     if (value == '3')
     return '预收款';
     if (value == '4')
     return '应收款';
     }
     },
     {field: 'pjlx_z', title: '票据类型', width: 15, align: 'center'},
     {field: 'nbkhh_qc', title: '入账银行', width: 15, align: 'center'},
     {field: 'shr_xm', title: '审核人', width: 12, align: 'center'},
     {field: 'sbgs_jc', title: '公司名称', width: 20, align: 'center'},
     {field: 'voucherNum', title: '凭证号', width: 15, align: 'center'},
     {field: 'czzt', title: '状态', width: 10, align: 'center', formatter: function (value, row, index) {
     if (value == 'wdr')
     return "未导入";
     if (value == 'drcg')
     return "导入成功";
     if (value == 'drsb')
     return "导入失败";
     if (value == 'sccg')
     return "删除成功";
     if (value == 'scsb')
     return "删除失败";
     return "";
     }}
     ]
     ],
     onLoadSuccess: function () {
     var rows = $('#grid_bzjdr').datagrid("getRows");
     if (rows.length > 0) {
     $('#grid_bzjdr').datagrid("selectRow", 0);
     }
     bzjdr.setBtnStatus();
     $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
     },
     onClickRow: function () {
     bzjdr.setBtnStatus();
     }
     });
     }*/

    /* //当类型切换时切换页面
     $("input[name='bzjdr_type']").change(function () {
     if ($("[name='bzjdr_type']:checked").val() == 1 || !$("[name='bzjdr_type']:checked").val()) {
     $("#kmgystype").text("供应商：");
     $('#grid_bzjdr').datagrid({
     url: $.webPath() + "/cwgl/bzjdr/bzjdrList?skflx=1",
     toolbar: "#toolbar_bzjdr",
     fitColumns: true,
     singleSelect: true,
     pagination: true,
     pageNumber: 1,
     fit: true,
     rownumbers: true,
     striped: true,
     columns: [
     [
     {field: 'bm', title: '单据号', width: 25, align: 'center'},
     {field: 'sfkrq', title: '付款日期', width: 25, align: 'center'},
     {field: 'sfklx', title: '付款类型', width: 15, align: 'center',
     formatter: function (value) {
     if (value == '1')
     return '供应付款';
     else
     return '销售退款';
     }
     },
     {field: 'gldw_qc', title: '关联企业', width: 30, align: 'center'},
     {field: 'sfje', title: '付款金额（元）', width: 20, align: 'right',
     formatter: function (value) {
     var sl = formatJe2(value, ",");
     return sl;
     },
     styler: function () {
     return 'color:green;font-size:14px;';
     }
     },
     {field: 'pjlx_z', title: '票据类型', width: 15, align: 'center'},
     {field: 'nbkhh_qc', title: '入账银行', width: 15, align: 'center'},
     {field: 'kxlx', title: '款项类型', width: 15, align: 'center',
     formatter: function (value) {
     if (value == '1')
     return '预付款';
     if (value == '2')
     return '应付款';
     if (value == '3')
     return '预收款';
     if (value == '4')
     return '应收款';
     }
     },
     {field: 'shr_xm', title: '审核人', width: 12, align: 'center'},
     {field: 'sbgs_jc', title: '公司名称', width: 20, align: 'center'},
     {field: 'voucherNum', title: '凭证号', width: 15, align: 'center'},
     {field: 'czzt', title: '状态', align: 'center', width: 10, formatter: function (value, row, index) {
     if (value == 'wdr')
     return "未导入";
     if (value == 'drcg')
     return "导入成功";
     if (value == 'drsb')
     return "导入失败";
     if (value == 'sccg')
     return "删除成功";
     if (value == 'scsb')
     return "删除失败";
     return "";
     }}
     ]
     ],
     onLoadSuccess: function () {
     var rows = $('#grid_bzjdr').datagrid("getRows");
     if (rows.length > 0) {
     $('#grid_bzjdr').datagrid("selectRow", 0);
     }
     bzjdr.setBtnStatus();
     $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
     },
     onClickRow: function () {
     bzjdr.setBtnStatus();
     }
     });
     } else if ($("[name='bzjdr_type']:checked").val() == 3 || !$("[name='bzjdr_type']:checked").val()) {
     $("#kmgystype").text("客户：");
     $('#grid_bzjdr').datagrid({
     url: $.webPath() + "/cwgl/bzjdr/bzjdrList?sfklx=3",
     toolbar: "#toolbar_bzjdr",
     fitColumns: true,
     singleSelect: true,
     pagination: true,
     fit: true,
     pageNumber: 1,
     rownumbers: true,
     striped: true,
     columns: [
     [
     {field: 'bm', title: '单据号', width: 25, align: 'center'},
     {field: 'sfkrq', title: '收款日期', width: 25, align: 'center'},
     {field: 'sfklx', title: '收款类型', width: 15, align: 'center',
     formatter: function (value) {
     if (value == '3')
     return '销售收款';
     else
     return '供应退款';
     }
     },
     {field: 'gldw_qc', title: '关联单位', width: 30, align: 'center'},
     {field: 'sfje', title: '付款金额（元）', width: 20, align: 'right',
     formatter: function (value) {
     var sl = formatJe2(value, ",");
     return sl;
     },
     styler: function () {
     return 'color:green;font-size:14px;';
     }
     },
     {field: 'kxlx', title: '款项类型', width: 15, align: 'center',
     formatter: function (value) {
     if (value == '1')
     return '预付款';
     if (value == '2')
     return '应付款';
     if (value == '3')
     return '预收款';
     if (value == '4')
     return '应收款';
     }
     },
     {field: 'pjlx_z', title: '票据类型', width: 15, align: 'center'},
     {field: 'nbkhh_qc', title: '入账银行', width: 15, align: 'center'},
     {field: 'shr_xm', title: '审核人', width: 12, align: 'center'},
     {field: 'sbgs_jc', title: '公司名称', width: 20, align: 'center'},
     {field: 'voucherNum', title: '凭证号', width: 15, align: 'center'},
     {field: 'czzt', title: '状态', align: 'center', width: 10, formatter: function (value, row, index) {
     if (value == 'wdr')
     return "未导入";
     if (value == 'drcg')
     return "导入成功";
     if (value == 'drsb')
     return "导入失败";
     if (value == 'sccg')
     return "删除成功";
     if (value == 'scsb')
     return "删除失败";
     return "";
     }}
     ]
     ],
     onLoadSuccess: function () {
     var rows = $('#grid_bzjdr').datagrid("getRows");
     if (rows.length > 0) {
     $('#grid_bzjdr').datagrid("selectRow", 0);
     }
     bzjdr.setBtnStatus();
     $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
     },
     onClickRow: function () {
     bzjdr.setBtnStatus();
     }
     });
     }
     });*/

//**********按钮触发事件**********

    //查询按钮
    $("#btn_bzjdr_search").click(function () {
        bzjdr_search();
    });

    //撤销
    $("#btn_bzjdr_cx").click(function () {
        bzjdr_cx();
    });

    //重置
    $("#btn_bzjdr_cz").click(function () {
        bzjdr_cz();
    });

    //加载查询条件中的申报公司下拉框
    $("#bzjdr_sbgs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
//    //加载票据类型下拉框
//    $("#table_bzjdr").find("[id='bzjdr_pjlx_id']").sjzdcombobox("pjlx");

    // 财务收付导入按钮
    $("#btn_bzjdr_import").click(function () {
        bzjdr_insert();
    });
});

// 导入窗口
function bzjdr_insert() {
    var row = $('#grid_bzjdr').datagrid('getSelected');
    if (row) {
        var win = $.window({
            title: "保证金导入凭证",
            width: 1100,
            height: 500,
            cache: false,
            url: $.webPath() + "/cwgl/bzjdr/bzjdr_content?bzjdrId=" + row.id,
            modal: true,
            buttons: [
                {
                    text: '确认',
                    handler: function () {
                        $(win).mask();
                        $.ajax({
                            url: $.webPath() + "/cwgl/bzjdr/generate_voucher",
                            dataType: 'json',
                            type: 'POST',
                            data: {
                                "attachment": $("#bzjdr_attachment").val(),
                                "bzjdrId": $("#bzjdr_id").val()},
                            beforeSend: function () {
                                if ($("#bzjdr_attachment").val() == "" || $("#bzjdr_attachment").val() == null) {
                                    $.messager.alert("提示", "请填写附单据数", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                            },
                            success: function (msg) {
                                $(win).mask('hide');
//                                $("#layout_bzjdr").mask("hide");
                                $(win).dialog('close');
                                if (msg.responseflag == 0) {
                                    $('#grid_bzjdr').datagrid('reload');
                                    $.messager.alert("提示", msg.responsemessage, "error");
                                } else {
                                    $('#grid_bzjdr').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: msg.responsemessage,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            }
                        });
                    }
                }
            ]
        });
    } else {
        $.messager.alert('提示', '请选择一行数据', 'error');
    }
}
//财务收付撤销
function bzjdr_cx() {
    var row = $('#grid_bzjdr').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否撤销该财务收付？', function (r) {
            if (r) {
                var index = $('#grid_bzjdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/bzjdr/revoke_voucher?bzjdrId=" + row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_bzjdr").mask();
                    },
                    success: function (msg) {
                        $("#layout_bzjdr").mask("hide");
                        if (msg.responseflag == 0) {
                            $('#grid_bzjdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#grid_bzjdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要撤销的收付款！", "warning");
    }
}


//贴现承兑重置
function bzjdr_cz() {
    var row = $('#grid_bzjdr').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否重置该收付款？', function (r) {
            if (r) {
                var index = $('#grid_bzjdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/bzjdr/rest_voucher?bzjdrId=" + row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_bzjdr").mask();
                    },
                    success: function (msg) {
                        $("#layout_bzjdr").mask("hide");
                        if (msg.responseflag == 0) {
                            $('#grid_bzjdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        }
                        else {
                            $('#grid_bzjdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的财务收付！", "warning");
    }
}

//查询财务收付
function bzjdr_search() {
    var bzjdrZt = $('#bzjdr_zt').combobox('getValue') == null ? null : $('#bzjdr_zt').combobox('getValue');
    $('#grid_bzjdr').datagrid('load',
        {
            bzjdrpjlxMc: $("#bzjdr_pjlx_id").val() || '',
            bm: $('#bzjdr_bm').val(),
            bzjdrdrZt: bzjdrZt,
            sfkrqStart: $("#bzjdr__sfkrq_start").datebox("getValue") || '',
            sfkrqEnd: $("#bzjdr_sfkrq_end").datebox("getValue") || '',
            gldwMc: $('#bzjdr_gldw').attr("_text") == null ? null : $('#bzjdr_gldw').attr("_text"),
            sbgsId: $('#bzjdr_sbgs').combotree('getValue') == null ? null : $('#bzjdr_sbgs').combotree('getValue'),
            sbgs: $('#bzjdr_sbgs').combotree('getText') == null ? null : $('#bzjdr_sbgs').combotree('getText')
        }
    );
}